// pages/ai/ai.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_id: 0,
    avatar: '',
    username: '微信用户',
    is_agree: false,
    showPop: false,
    privacyContractName: '《智能问答小程序隐私保护指引》',
    bannerList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.request({
      url: getApp().globalData.ai_host + '/index/Api/bannnerList',
      success: res => {
        if (res.data.result === 1) {
          this.setData({
            bannerList: res.data.data
          })
        }
      }
    })

    const user_id = wx.getStorageSync('user_id');
    if (!user_id) {
      wx.login({
        success: res => {
          const code = res.code;
          wx.request({
            url: getApp().globalData.ai_host + '/index/Api/codeGetOpenidToUserInfo',
            data: {
              code
            },
            success: res => {
              if (res.data.result === 1) {
                const {
                  id,
                  avatar,
                  username
                } = res.data.data;

                this.setData({
                  user_id: id,
                  avatar: avatar,
                  username: username
                })
              }
            }
          })
        }
      })
    }
  },
  closePop: function () {
    this.setData({
      showPop: false
    })
  },

  getAvatar: function (e) {
    const avatar = e.detail.avatarUrl;
    wx.uploadFile({
      url: getApp().globalData.ai_host + '/Upload/UploadFile/uniUploadPicture',
      filePath: avatar,
      name: 'download',
      success: res => {
        const data = JSON.parse(res.data);
        if (data.result === 1) {
          this.setData({
            avatar: data.data.path
          })
        } else {
          wx.showToast({
            title: '上传图片失败',
            icon: 'none'
          })
        }
      }
    })
  },

  getUsername: function (e) {
    this.setData({
      username: e.detail.value
    })
  },

  showPrivacyModel: function () {
    wx.openPrivacyContract();
  },

  userLogin: function () {
    const is_agree = this.data.is_agree
    if (!is_agree) {
      wx.showToast({
        title: '请阅读并勾选隐私协议',
        icon: 'none'
      })
    } else {
      wx.showLoading({
        title: '登录中...'
      })

      const avatar = this.data.avatar,
        username = this.data.username;

      wx.login({
        success: res => {
          const code = res.code;

          wx.request({
            url: getApp().globalData.ai_host + '/index/Api/wxLogin',
            data: {
              avatar,
              username,
              code
            },
            success: result => {
              wx.hideLoading();
              if (result.data.result === 1) {
                const {
                  id,
                  avatar
                } = result.data.data,
                  type = this.data.type;

                wx.setStorageSync('user_id', id);
                wx.setStorageSync('avatar', avatar);

                wx.showToast({
                  title: '登录成功'
                })

                this.setData({
                  showPop: false
                })

                wx.navigateTo({
                  url: `/pages/chat/chat?type=${type}`
                })
              } else {
                wx.showToast({
                  title: result.data.msg,
                  icon: 'none'
                })
              }
            }
          })
        }
      });
    }
  },

  doAgree: function () {
    this.setData({
      is_agree: !this.data.is_agree
    })
  },

  goAi: function (e) {
    var type = e.currentTarget.dataset.type;
    const user_id = wx.getStorageSync('user_id');
    if (user_id) {
      wx.navigateTo({
        url: `/pages/chat/chat?type=${type}`
      })
    } else {
      this.setData({
        showPop: true,
        type: type
      })
    }

  },
})